
#include "Game.h"



Game::Game()
: m_root( NULL )
, m_window( NULL )
, m_camera( NULL )
, m_viewport( NULL )
, m_sceneManager( NULL )
, m_exitRequested( false )
{
}

Game::~Game()
{
	InputManager::finalise();

	delete m_root;
}



void Game::initialise()
{
	m_root = new Ogre::Root( "ogre_plugins.txt", "ogre_configuration.txt", "ogre_log.txt" );

	// Initialise display options.
	// It shows the ugly dialog at start-up, so if you don't want to see it, it's up to you
	// to remove this line and initialise display options manually ( e.g. reading them from
	// a text file ).
	bool configDialogUserContinue = m_root->showConfigDialog();
	if ( ! configDialogUserContinue )
	{
		throw std::exception( "User closed/canceled config dialog" );
	}

	// Create window with chosen display options.
	m_window = m_root->initialise( true, "Ogre Window" );

	m_root->addFrameListener(this);

	InputManager::initialise( m_window );
	loadResources();

	createScene();
}

void Game::loadResources()
{
	// Load resources specified in "ogre_resources.txt" file

	ConfigFile cf;
	cf.load("ogre_resources.txt");

	ConfigFile::SectionIterator seci = cf.getSectionIterator();
	String secName, typeName, archName;
	while (seci.hasMoreElements())
	{
		secName = seci.peekNextKey();
		ConfigFile::SettingsMultiMap *settings = seci.getNext();
		ConfigFile::SettingsMultiMap::iterator i;
		for (i = settings->begin(); i != settings->end(); ++i)
		{
			typeName = i->first;
			archName = i->second;
			ResourceGroupManager::getSingleton().addResourceLocation(archName, typeName, secName);
		}
	}

	ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
}

void Game::createScene()
{
	// Need specific SceneManager if we want to render terrain
	m_sceneManager = m_root->createSceneManager(Ogre::ST_EXTERIOR_CLOSE, "MainManager");

	m_sceneManager->setAmbientLight( ColourValue( 1, 1, 1 ) );

	head = m_sceneManager->createEntity("OgreHead", "ogrehead.mesh");
	sceneNode = m_sceneManager->getRootSceneNode()->createChildSceneNode();

	sceneNode->attachObject(head);
	sceneNode->setPosition(Vector3(0, 0, 0));

	m_camera = m_sceneManager->createCamera("Default Camera");

	m_camera->setPosition(Vector3(0, 10, 100));
	m_camera->lookAt(Vector3(0, 0, 0));
	m_camera->setNearClipDistance(0.01);

	m_viewport = m_window->addViewport(m_camera);

}

void Game::go()
{
	bool continueRunning = true;
	while ( continueRunning )
	{


		InputManager::capture();

		// Update logic stuff
		//updateLogic();

        m_root->renderOneFrame();
		continueRunning &= !m_exitRequested;
	}
}



// INPUTMANAGER
bool Game::keyPressed( const OIS::KeyEvent& e )
{
	if(e.key == OIS::KC_ESCAPE)
	{

		m_exitRequested = true;

	}

	return true;
}

bool Game::mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
 {
 	return true;
 }

 bool Game::mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
 {
 	return true;
 }


bool Game::mouseMoved( const OIS::MouseEvent& e )
{
	return true;
}


// FRAMELISTENER
bool Game::frameStarted(const FrameEvent& evt)
{
	return true;
}

bool Game::frameEnded(const FrameEvent& evt)
{
	return true;
}
